package com.example.administrator.myeventbus30;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class Main2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        //注册eventbus
        EventBus.getDefault().register(this);
    }

    //接受的方法，第一个参数表示在哪个线程中执行以下代码，sticky表示是否是粘性的
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void getEventBus(Thing thing){
        Toast.makeText(this, thing.a+this.getClass().getSimpleName(), Toast.LENGTH_SHORT).show();
        EventBus.getDefault().removeStickyEvent(thing);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getAEvent(Thing thing){
        Toast.makeText(this, thing.a+this.getClass().getSimpleName(), Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
